package list;

public class StackLinkedListDemo {
        public static void main(String[] args) {
        StackLinkedList stackLinkedList = new StackLinkedList(5);
        stackLinkedList.showStack();
        stackLinkedList.push(10);
        stackLinkedList.push(20);
        stackLinkedList.showStack();
        int res = stackLinkedList.pop();
        System.out.println("出栈："+res);
        res = stackLinkedList.pop();
        System.out.println("出栈："+res);
        res = stackLinkedList.pop();
        System.out.println("出栈："+res);
        //stackLinkedList.push(50);

        }
}
class StackLinkedList {
    private int top;
    private int bottom;
    private int maxSize;
    private StackNode stack = new StackNode(0);

    public StackLinkedList(int maxSize) {
        this.maxSize = maxSize;
    }

    public void push(int val) {
        if (top == maxSize) {
            System.out.println("栈满~");
            return;
        }
        StackNode cur = stack;
        StackNode newStack = new StackNode(val);
        for (int i = 0; i < top; i++) {
            cur = cur.getNext();
        }
        cur.setNext(newStack);
        top++;
    }

    public int pop() {
        if(top == bottom) {
            System.out.println("栈空~");
            return -1;
        }
        StackNode cur = stack;
        for(int i = 0; i < top; i++) {
            cur = cur.getNext();
        }
        top--;
        int temp = cur.getVal();
        cur = null;
        return temp;
    }
    public void showStack() {
        if (top == bottom) {
            System.out.println("栈空~");
            return;
        }
        StackNode cur = stack.getNext();
        for (int i = 0; i < top; i++) {
                System.out.printf("%d\n", cur.getVal());
                cur = cur.getNext();
        }
    }
}
class  StackNode{
    private int val;
    private StackNode next;

    public StackNode(int val) {
        this.val = val;
    }

    public int getVal() {
        return val;
    }
    public StackNode getNext() {
        return next;
    }

    public void setNext(StackNode next) {
        this.next = next;
    }
}